<template>
  <div class="rules" v-if="config">
    <div class="rules-title">
      <span class="title">
        {{ $srv.getI18nByPath('piece.oHeaderGroupSetting.headergrouping') }}
      </span>
      <Button size="small" @click="addHeaderGroup">
        {{ $srv.getI18nByPath('piece.oHeaderGroupSetting.addgroup') }}
      </Button>
    </div>

    <div class="rules-cell" v-for="(item, idx) of config" :key="idx">
      <div class="rules-cell-title">
        <span>
          {{ $srv.getI18nByPath('piece.oChartSetting.numbering') }}
          :{{ idx + 1 }}
        </span>
        <i-input
          :placeholder="
            $srv.getI18nByPath(
              'piece.oHeaderGroupSetting.pleaseenterthegroupname'
            )
          "
          v-model="item.title"
          @on-blur="saveChanges"
        ></i-input>
        <div class="rules-cell-btns">
          <Button size="small" @click="addHeaderGroupChildren(idx)">
            {{
              $srv.getI18nByPath('piece.oHeaderGroupSetting.increasemembers')
            }}
          </Button>
          <Icon type="md-close" @click="removeHeaderGroup(idx)" />
        </div>
      </div>
      <template v-if="item.children">
        <div
          class="rules-cons header-group-children"
          v-for="(child, iidx) of item.children"
          :key="'child' + iidx"
        >
          <div class="rules-cons-inputs">
            <div class="rules-cons-fields">
              <i-input
                :placeholder="
                  $srv.getI18nByPath('piece.oChartSetting.fieldname') +
                    ' / ' +
                    $srv.getI18nByPath('piece.oHeaderGroupSetting.groupnumber')
                "
                v-model="child.field"
                @on-blur="saveChanges"
              ></i-input>
            </div>
          </div>
          <div class="rules-res-btns">
            <Icon
              type="md-close"
              @click="removeHeaderGroupChildren(idx, iidx)"
            />
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Model, Vue } from 'vue-property-decorator';

@Component
export default class oHeaderGroupSetting extends Vue {
  @Model('modelchange') config: any;

  mounted() {}

  private saveChanges() {
    this.$emit('modelchange', this.config);
    this.$emit('change');
  }

  /**
   * 移除表头分组
   */
  private removeHeaderGroup(index: number) {
    this.config.splice(index, 1);
    this.saveChanges();
  }

  /**
   * 移除表头分组子节点
   */
  private removeHeaderGroupChildren(index: number, subIndex: number) {
    this.config[index].children.splice(subIndex, 1);
    this.saveChanges();
  }

  /**
   * 增加表头分组
   */
  private addHeaderGroup() {
    this.config.push({
      title: '',
      children: []
    });
    this.saveChanges();
  }

  /**
   * 增加表头分组子节点
   */
  private addHeaderGroupChildren(index: number) {
    this.config[index].children.push({
      field: ''
    });
    this.saveChanges();
  }
}
</script>

<style lang="scss" scoped>
.rules {
  .rules-title {
    width: 100%;
    height: 40px;
    .title {
      float: left;
    }
    button {
      float: right;
      margin: 6px 0;
    }
  }
  .rules-cell {
    background-color: #fdfdfd;
    padding: 10px;
    margin-bottom: 10px;
    .ivu-icon {
      cursor: pointer;
    }
    .rules-cell-title ::v-deep {
      width: 100%;
      height: 30px;
      line-height: 30px;
      .ivu-input-wrapper {
        width: 130px;
        margin-left: 15px;
        .ivu-input {
          background-color: transparent;
          border: 0;
        }
      }
      span {
        float: left;
      }
      .rules-cell-btns {
        float: right;
        button {
          margin-right: 10px;
        }
      }
    }
    .rules-cons {
      margin: 10px 0;
      display: flex;
      .rules-cons-title {
        line-height: 30px;
        flex-shrink: 0;
      }
      .rules-cons-inputs {
        margin: 0 10px;
        flex: 1;
        .rules-cons-fields {
          margin-bottom: 10px;
        }
        .rules-cons-fields,
        .rules-cons-values {
          display: flex;
          > div:first-child {
            margin-right: 10px;
          }
        }
      }
      .rules-cons-btns {
        flex-shrink: 0;
        line-height: 30px;
      }
    }
    .header-group-children {
      .rules-cons-inputs {
        margin-left: 0;
        .rules-cons-fields {
          margin-bottom: 5px;
          > div:first-child {
            margin-right: 0;
          }
        }
      }
    }
    .rules-res {
      margin: 10px 0;
      display: flex;
      .rules-res-title {
        line-height: 30px;
        flex-shrink: 0;
      }
      .rules-res-inputs {
        margin: 0 10px;
        flex: 1;
        .rules-res-type {
          margin-bottom: 10px;
        }
        .rules-res-details {
          display: flex;
          flex-direction: column;
          > div {
            display: flex;
            justify-content: space-between;
            margin: 5px 0 10px;
          }
        }
      }
      .rules-res-btns {
        flex-shrink: 0;
        line-height: 30px;
      }
    }
  }
}
</style>
